package com.and.xmppsdk;

import org.drl.xmpp.TalkClient;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;

import com.and.xmppsdk.fragment.FragmentIndicator;
import com.and.xmppsdk.fragment.FragmentIndicator.OnIndicateListener;
import com.and.xmppsdk.fragment.panel.ExitPanel;

public class MainActivity extends BaseActivity {

	TalkClient talk;
	private Fragment mFragments[] = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		setFragmentIndicator(0);
	}


	/**
	 * 设置当前选中布局，隐藏其他
	 * 
	 * @param whichIsDefault
	 */
	private void setFragmentIndicator(int whichIsDefault) {
		mFragments = new Fragment[3];
		mFragments[0] = getSupportFragmentManager().findFragmentById(
				R.id.filesFragment);
		mFragments[1] = getSupportFragmentManager().findFragmentById(
				R.id.tasksFragment);
		mFragments[2] = getSupportFragmentManager().findFragmentById(
				R.id.aboutFragment);
		getSupportFragmentManager().beginTransaction().hide(mFragments[0])
				.hide(mFragments[1]).hide(mFragments[2])
				.show(mFragments[whichIsDefault]).commit();

		final FragmentIndicator mIndicator = (FragmentIndicator) findViewById(R.id.fragmentIndicator);
		mIndicator.setIndicator(whichIsDefault);
		mIndicator.setOnIndicateListener(new OnIndicateListener() {
			@Override
			public void onIndicate(View view, int which) {
				
				getSupportFragmentManager().beginTransaction()
						.hide(mFragments[0]).hide(mFragments[1])
						.hide(mFragments[2]).show(mFragments[which]).commit();
			}
		});
	}

	private ExitPanel mExitPanel;

	private void showExitPanel() {
		FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
		Fragment prev = getSupportFragmentManager().findFragmentByTag(
				ExitPanel.TAG);
		if (prev != null) {
			ft.remove(prev);
		}
		ft.addToBackStack(null);

		mExitPanel = ExitPanel.getInstance();
		mExitPanel.show(ft, ExitPanel.TAG);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			showExitPanel();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
